//
//  LookUseImageViewController.m
//  WULIHH
//
//  Created by tang on 16/5/13.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "LookUseImageViewController.h"
#import "Header.h"
#import "MJRefresh.h"
#import "WuLiUserWorkReauest.h"

#import "StarWorkCollectionViewCell.h"
#import "ShowDetalImageViewController.h"
@interface LookUseImageViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    UICollectionView *mainCollectionView;
    NSMutableArray *dataMutableArray;
    
    NSString *pageString;
}

@end

@implementation LookUseImageViewController


- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:uicolor(237, 237, 237)];
    dataMutableArray = [NSMutableArray new];
    pageString = @"1";
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WithView, 64.0)];
    [self.view addSubview:backView];
    [backView setBackgroundColor:uicolor(253, 202,47)];
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(WithView / 2.0 - 50, 26.0, 100, 30)];
//    NSLog(@"%@",_titleNameStr);
    [titleLable setText:@"引用作品"];
    [titleLable setFont:[UIFont systemFontOfSize:16.0]];
    [titleLable setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:titleLable];
    
    
    UIImage *image = nil;
    image = [UIImage imageNamed:@"Detalback.png"];
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 32, 10, 18)];
    [backButton setBackgroundImage:image forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(backButton.frame)+6, 31.0, 50.0, 20.0)];
    [lable setText:@"返回"];
    [lable setFont:[UIFont systemFontOfSize:15.0]];
    [lable setTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:lable];
    
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
    [self.view addSubview:back];
    [back addTarget:self action:@selector(popTOVIEW) forControlEvents:UIControlEventTouchUpInside];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WithView, heightView-64.0) collectionViewLayout:flowLayout];
    [mainCollectionView setBackgroundColor:uicolor(221, 221, 221)];
    [self.view addSubview:mainCollectionView];


}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [mainCollectionView setDataSource:self];
    [mainCollectionView setDelegate:self];
    [mainCollectionView registerClass:[StarWorkCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lookUseWorkGetBack:) name:@"yinyong" object:nil];
    
    mainCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewMyWorkioData)];
    mainCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreMyWorkioData)];
    [mainCollectionView.mj_header beginRefreshing];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)popTOVIEW
{
    [self.navigationController popViewControllerAnimated:YES];
}


//刷新
- (void)loadNewMyWorkioData
{
    [WuLiUserWorkReauest requestLookUseImage:_imageIDString withPage:pageString];
}

//加载更多
- (void)loadMoreMyWorkioData
{
    pageString = [NSString stringWithFormat:@"%d", [pageString intValue]+1];
    [WuLiUserWorkReauest requestLookUseImage:_imageIDString withPage:pageString];
}


- (void)lookUseWorkGetBack:(NSNotification *)notion
{
    [mainCollectionView.mj_header endRefreshing];
    [mainCollectionView.mj_footer endRefreshing];
    
    NSDictionary *dic = [notion userInfo];
//    NSLog(@"%@", dic);
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        if ([pageString isEqualToString:@"1"])
        {
            [dataMutableArray removeAllObjects];
        }
        else
        {
            if ([[dic valueForKey:@"data"] count] == 0)
            {
                pageString = [NSString stringWithFormat:@"%d", [pageString intValue]-1];
            }

        }
        [dataMutableArray addObjectsFromArray:[dic valueForKey:@"data"]];
        [mainCollectionView reloadData];
    }
    else
    {
        
    }
}





-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(4, 5, 4, 5);
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    NSLog(@"%d", dataMutableArray.count);
    return dataMutableArray.count;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(biliwith(146.0), biliwith(242.0));
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"cell";
    StarWorkCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setTag:indexPath.row];
    [cell loadModel:dataMutableArray[indexPath.row]];
    return cell;
}



//进入作品详情
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"data ===== %@",dataMutableArray);
    //    NSLog(@"%@", dataMutableArray[indexPath.row]);
    ShowDetalImageViewController *detalView = [[ShowDetalImageViewController alloc] init];
    detalView.IDString = [NSString stringWithFormat:@"%@", [[dataMutableArray objectAtIndex:indexPath.row] valueForKey:@"id"]];
    detalView.textStr = [[dataMutableArray objectAtIndex:indexPath.row]valueForKey:@"title"];
    [self.navigationController pushViewController:detalView animated:YES];
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
